//
//  UmRechargeViewController.m
//  PlayBall
//
//  Created by Di DaDi on 13-11-12.
//
//

#import "JddUmRechargeViewController.h"
#import "JddViewControllerManager.h"
#import "JddQueryUmTradNoOperation.h"
#import "HUD.h"
#import "AppDelegate.h"

@interface JddUmRechargeViewController ()
{
    AppDelegate *appDelegate;
    UILabel *label;
}

@end

@implementation JddUmRechargeViewController

SHARED_VIEW_CONTROLLER_IMPLEMENT(JddUmRechargeViewController);

@synthesize userNameLabel;
@synthesize amountTextField;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
   // [self.navigationItem setTitle:PAGE_UM_RECHARGE];
    
        UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
        customLab.backgroundColor = [UIColor clearColor];
        [customLab setText:PAGE_UM_RECHARGE];
        customLab.textAlignment = NSTextAlignmentCenter;

        [customLab setTextColor:[UIColor whiteColor]];
        customLab.font = [UIFont systemFontOfSize:19];
        self.navigationItem.titleView = customLab;
        
        //self.navigationItem.hidesBackButton = YES;
    
    appDelegate=[[UIApplication sharedApplication] delegate];
    
}

- (void)createlabel
{
    label = [[UILabel alloc] initWithFrame:CGRectMake(20, 180, 320, 40)];
    label.text = [NSString stringWithFormat:@"充值%@彩豆，送红包%@彩豆!",appDelegate.moneyNum,appDelegate.redNum];
    label.textColor = [UIColor colorWithRed:140/255.0f green:140/255.0f blue:140/255.0f alpha:1];
    [self.view addSubview:label];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)QueryUmTradNoOperationResult:(NSData *)result{
    [HUD hideUIBlockingIndicator];
    if(result){
        [JddCommonFunction setWaitingViewHidden:YES fromController:self];
        SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
        NSDictionary *dictResult = [jsonParser objectWithData:result];
        NSInteger code = [[dictResult objectForKey:@"Code"] intValue];
        NSString *data = [dictResult objectForKey:@"Data"];
        if (!(code == 0 ? YES : NO)) {
            [HUD showToast:data];
            return;
        }
        NSString *tradeNo = [JddDataParser parseBuyHistoryDetailResult:result];
        [self pay:tradeNo];
    }else{
        //生成账单号失败
        [JddCommonFunction messageBoxWithMessage:@"获取账单号失败"];
    }
}

-(void)pay:(NSString *)tradeNo{
    Boolean isnull = false;
    if([JddUserProfile sharedInstance].realName && [JddUserProfile sharedInstance].IDCard){
        isnull = false;
    }else{
        isnull = true;
    }
    if (tradeNo && ![@"" isEqual:tradeNo]) {
        
//        if (isnull) {
//            [Umpay pay:tradeNo merCustId:nil shortBankName:nil cardType:@"1" payDic:nil rootViewController:self delegate:self];
//        }else{
//            UmpayElements *inPayment = [[UmpayElements alloc] init];
//            [inPayment setIdentityCode:[JddUserProfile sharedInstance].IDCard];
//            [inPayment setEditFlag:@"0"];
//            [inPayment setCardHolder:[JddUserProfile sharedInstance].realName];
//            [Umpay pay:tradeNo merCustId:nil shortBankName:nil cardType:@"1" payDic:inPayment rootViewController:self delegate:self];
//        }
        
        //        [Umpay pay:tradeNo payType:@"9" rootViewController:self.view.window.rootViewController delegate:self];
        ////        [Umpay pay:tradeNo payType:@"9" window:self.view.window delegate:self];
    }else{
        [JddCommonFunction messageBoxWithMessage:@"获取账单号失败"];
    }
}

-(void) onResult:(NSString *)orderId resultCode:(NSString *)resultCode resultMessage:(NSString *)resultMessage{
    NSString *message = nil;
    if([resultCode isEqual:@"0000"]){
        message = [NSString stringWithFormat:@"您已经成功充值%@彩豆",amountTextField.text];
        [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_USER_CENTER
                                                                fromController:nil
                                                                    withObject:nil
                                                                   withObject2:nil];
    }else if([resultCode isEqual:@"1001"]){
        message = @"您已取消充值";
    }else if ([resultCode isEqual:@"1002"]){
        message = [NSString stringWithFormat:@"充值失败:%@",resultMessage];
    }
    [HUD showToast:message];
}

-(void)onPayResult:(NSString *)orderId resultCode:(NSString *)resultCode resultMessage:(NSString *)resultMessage{
    
    NSString *message = nil;
    if([resultCode isEqual:@"0000"]){
        message = [NSString stringWithFormat:@"您已经成功充值%@彩豆",amountTextField.text];
        [[JddViewControllerManager sharedInstance] pushViewControllerWithIndex:CONTROLLER_USER_CENTER
                                                                fromController:nil
                                                                    withObject:nil
                                                                   withObject2:nil];
    }else if([resultCode isEqual:@"1001"]){
        message = @"您已取消充值";
    }else if ([resultCode isEqual:@"1002"]){
        message = [NSString stringWithFormat:@"充值失败:%@",resultMessage];
    }
    [JddCommonFunction messageBoxWithMessage:message];
}



- (void)backfirst:(id)sender {
    [amountTextField resignFirstResponder];
}

- (IBAction)rechargeButtonClicked:(id)sender {
    [amountTextField resignFirstResponder];
    if (![amountTextField.text length]) {
        [JddCommonFunction messageBoxWithMessage:@"请输入金额"];
        return;
    }
    else if ([amountTextField.text intValue]<1) {
        [JddCommonFunction messageBoxWithMessage:@"每次充值不能少于1彩豆"];
        return;
    }
    //获取订单号
    JddQueryUmTradNoOperation *operation = [[JddQueryUmTradNoOperation alloc] init];
    operation.delegate = self;
    operation.Money = [NSNumber numberWithInt:[amountTextField.text intValue]];
    [[JddGlobalParameter sharedInstance].operationQueue addOperation:operation];
    [HUD showUIBlockingIndicatorWithText2:@"请求中..." withTimeout:30];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"chongzhi"];
	[MobClick event:@"UserCenter-chongzhi"];

    self.userNameLabel.text = [JddUserProfile sharedInstance].name;
    amountTextField.userInteractionEnabled = YES;
    self.amountTextField.text = appDelegate.moneyNum;
    if ([appDelegate.redNum length]!= 0 ) {
        [self createlabel];
        amountTextField.userInteractionEnabled = NO;
    }
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"chongzhi"];

    amountTextField.text = @"";
    [amountTextField resignFirstResponder];
    [label removeFromSuperview];
}

- (void)viewDidUnload {
    [self setUserNameLabel:nil];
    [self setAmountTextField:nil];
    [super viewDidUnload];
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
    return (toInterfaceOrientation == UIInterfaceOrientationMaskPortrait);
}
@end
